package com.zxw.hotel.common;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;

import java.time.Instant;
import java.util.Map;
import java.util.UUID;

public class JwtUtils {
    /**
     * @param payload 有效载荷，主体数据
     * @param secret  密钥，服务器保存
     * @return jwt令牌
     */

    public static String encode(String admin, Map<String,Object> payload, String secret) {

        JWTCreator.Builder builder = JWT.create();
        //添加数据
        builder.withAudience(admin);
        //过期时间
        builder.withExpiresAt(Instant.now().plusSeconds(1800));
        //颁发时间
        builder.withIssuedAt(Instant.now());
        //颁发人
        builder.withIssuer("admin");
        //令牌编号
        builder.withSubject(UUID.randomUUID().toString());
        //主题
        builder.withSubject("jwt");
        //何时之前不生效
        builder.withNotBefore(Instant.now());
        //添加有效载荷
        builder.withPayload(payload);
        //加密签名
        return builder.sign(Algorithm.HMAC256(secret));

    }
public static DecodedJWT decode(String token, String secret) {
        return JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
}

//    public static void main(String[] args) {
//        PasswordEncryptor encryptor = new StrongPasswordEncryptor();
//        String password = "123456";
//        String encryptPassword = encryptor.encryptPassword(password);
//        System.out.println(encryptPassword);
//    }
}